<template>
  <div>
    <div v-if="isAuthorized">
      <div class="uploadBox" v-show="!isGotData">
        <el-upload
            class="upload-demo"
            drag
            accept="image/*"
            ref="upload"
            :action=uploadUrl
            :auto-upload="false"
            :on-success="handleSuccess"
            multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        </el-upload>
        <el-button @click="submitUpload" style="margin-bottom: 30px">上传</el-button>
        <el-button @click="getHistory" style="margin-bottom: 30px">上传历史</el-button>
        <br>
        <transition name="el-fade-in-linear">
          <div class="uploadMsgBox" v-if="isSuccess">
            <el-table
                :data="successData"
                style="width: 100%;margin: 0">
              <el-table-column
                  prop="old_file_name"
                  label="原文件名称"
                  width="180">
              </el-table-column>
              <el-table-column
                  prop="new_file_name"
                  label="新文件名称"
                  width="180">
              </el-table-column>
              <el-table-column
                  prop="file_path"
                  label="文件路径"
                  width="880">
                <template slot-scope="scope">
                  <el-link :href="scope.row.file_path" target="_blank">{{ scope.row.file_path }}</el-link>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div v-else>{{ failMsg }}</div>
        </transition>
      </div>
      <div class="history" v-show="isGotData">
        <div class="search-Box">
          <el-input placeholder="请输入搜索关键字" icon="search" class="search" v-model="search"></el-input>
        </div>
        <br>
        <el-table
            :default-sort = "{prop: 'upload_time', order: 'descending'}"
            :data="historyData"
            style="width: 100%">
          <el-table-column
              prop="pk"
              label="pk"
              width="80">
          </el-table-column>
          <el-table-column
              prop="origin_image_name"
              label="原文件名称">
          </el-table-column>
          <el-table-column
              prop="new_image_name"
              label="新文件名称">
          </el-table-column>
          <el-table-column
              prop="image_size"
              label="文件大小(kb)">
          </el-table-column>
          <el-table-column
              prop="upload_time"
              label="上传时间" sortable>
          </el-table-column>
          <el-table-column
              prop="image_path"
              label="文件路径"
              width="880">
            <template slot-scope="scope">
              <el-link :href="scope.row.image_path" target="_blank">{{ scope.row.image_path }}</el-link>
            </template>
          </el-table-column>
          <el-table-column
              fixed="right"
              label="操作">
            <template slot-scope="scope">
              <el-button @click="viewPic(scope.row.image_path)" type="text" size="small">查看</el-button>
              <el-button @click="deletePic(scope.row.pk)" type="text" size="small">删除</el-button>
              <el-button @click="hiddenPic(scope.row.pk)" type="text" size="small">隐藏</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-button type="primary" icon="el-icon-close" circle
                 class="closeBtn"
                 @click="closeHistory"
                 v-show="isGotData"></el-button>
    </div>
    <div v-else>
      <el-dialog
          title="验证"
          :visible.sync="dialogVisible"
          width="30%">
        <el-input v-model="authCode" placeholder="请输入验证码" @keyup.enter.native=goAuth(authCode)></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="goAuth(authCode)">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <el-dialog
        :lock-scroll="false"
        :destroy-on-close="true"
        :visible.sync="picViewVisible"
        center>
      <el-image
          :src="picUrl"
          :preview-src-list="[picUrl]">
      </el-image>
    </el-dialog>
  </div>
</template>

<script>
import {goAuthAPI} from "@/components/utils/api";

export default {
  name: "image_bed_upload",
  computed: {
    historyData: function () {
      let search = this.search;
      if (search) {
        return this.tableData.filter(function (dataNews) {
          return Object.keys(dataNews).some(function (key) {
            return String(dataNews[key]).toLowerCase().indexOf(search) > -1
          })
        })
      }
      return this.tableData
    }
  },
  data() {
    return {
      requestUrlPrefix: this.$imageBedReUrl + 'bedapi/v1',
      uploadUrl: this.$imageBedReUrl + "bedapi/v1/upload/",
      // requestUrlPrefix: "http://127.0.0.1:8000/bed/bedapi/v1",
      // uploadUrl: "http://127.0.0.1:8000/bed/bedapi/v1/upload/",
      successData: [],
      tableData: [],
      failMsg: '',
      isGotData: false,
      isSuccess: false,
      search: '',
      isAuthorized: false,
      dialogVisible: true,
      authCode: '',
      picViewVisible: false,
      picUrl: '',
    }
  },
  methods: {
    hiddenPic(id) {
      let obj = [id]
      this.$post(this.requestUrlPrefix + '/hidden/', obj).then(res => {
        if (res.res_code === 200) {
          this.$message({
            message: '已隐藏',
            type: 'success'
          });
          this.getHistory()
        }else {
          this.$message.error(res.res_msg);
        }
      }).catch(err => {
        this.$message.error(err);
      })
    },
    deletePic(id) {
      let obj = [id]
      this.$post(this.requestUrlPrefix + '/delete/', obj).then(res => {
        if (res.res_code === 200) {
          this.$message({
            message: '已删除',
            type: 'success'
          });
          this.getHistory()
        }else {
          this.$message.error(res.res_msg);
        }
      }).catch(err => {
        this.$message.error(err);
      })
    },
    // 查看图片
    viewPic(filePath) {
      if (filePath) {
        this.picViewVisible = true
        this.picUrl = filePath
      }
    },
    goAuth(authCode) {
      goAuthAPI(authCode).then(res => {
        if (res.res_code === 0) {
          if (res.result) {
            this.$message({
              message: '验证成功',
              type: 'success'
            });
          } else {
            this.$message({
              message: '验证失败',
              type: 'error'
            });
          }
          this.isAuthorized = res.result
        } else {
          this.$message.error(res.res_msg);
        }
      }).catch(err => {
        this.$message.error(err);
      })
    },
    closeHistory() {
      this.isGotData = false
      this.search = ''
    },
    getHistory() {
      this.historyData = []
      this.$get(this.requestUrlPrefix + '/get_history/').then(res => {
        if (res.res_code === 0) {
          this.tableData = res.data
          this.isGotData = true
        } else {
          this.$message.error(res.res_msg)
        }
      }).catch(err => {
        this.$message.error(err)
      })
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleSuccess(res) {
      if (res.res_code === 0) {
        this.successData.push(res.file_info)
        this.isSuccess = true
      } else {
        this.failMsg = res.res_msg
        this.$message.error(res.res_msg)
      }
    }
  }
}
</script>

<style scoped>
.upload-demo {
  margin-bottom: 20px;
}

/*上下左右居中*/
.uploadBox {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.closeBtn {
  position: fixed;
  right: 10px;
  top: 10px;
}
</style>